package com.lzb.watermarkcamera.utils;

import androidx.annotation.WorkerThread;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.lzb.watermarkcamera.basic.picselect.SelectMediaEntity;
import com.lzb.watermarkcamera.bean.PhoneAlbumBean;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;


public class MyMediaLoader {

    private static MyMediaLoader sInstance;
    private final List<PhoneAlbumBean> classBeanList = new ArrayList<>();

    private MyMediaLoader() {

    }

    public synchronized static MyMediaLoader getInstance() {
        if (sInstance == null) {
            sInstance = new MyMediaLoader();
        }
        return sInstance;
    }

    @WorkerThread
    public List<PhoneAlbumBean> loadPicData(List<SelectMediaEntity> listData, boolean forceAgainGet) {
        if (!forceAgainGet) {
            if (classBeanList.size() > 0) {
                return classBeanList;
            }
        }
        classBeanList.clear();

        Map<String, List<PhoneAlbumBean.ClassBean>> timeCategoryMap = new HashMap<>();
        for (SelectMediaEntity file : listData) {
            String phoneTime = TimeUtils.millis2String(FileUtils.getFileLastModified(file.getPath()), "yyyy年MM月dd日");

            List<PhoneAlbumBean.ClassBean> beanList = null;
            if (timeCategoryMap.containsKey(phoneTime)) {
                beanList = timeCategoryMap.get(phoneTime);
            } else {
                beanList = new ArrayList<>();
                timeCategoryMap.put(phoneTime, beanList);
            }
            PhoneAlbumBean.ClassBean classBean = new PhoneAlbumBean.ClassBean(FileUtils.getFileName(file.getPath()),
                    file.getPath(), FileUtils.getFileLastModified(file.getPath()));
            beanList.add(classBean);
        }
        Iterator<String> iterator = timeCategoryMap.keySet().iterator();
        String time = null;
        while (iterator.hasNext()) {
            time = iterator.next();
            classBeanList.add(new PhoneAlbumBean(time, timeCategoryMap.get(time)));
        }
        SortUtils.getSortShotBefore(classBeanList);
        return classBeanList;
    }

    @WorkerThread
    public List<PhoneAlbumBean> loadPicData(boolean forceAgainGet, String path) {
        if (!forceAgainGet) {
            if (classBeanList.size() > 0) {
                return classBeanList;
            }
        }
        classBeanList.clear();

        List<File> files = getHistoryPic(path);

        Map<String, List<PhoneAlbumBean.ClassBean>> timeCategoryMap = new HashMap<>();
        for (File file : files) {

//            // 删除超过7天的文件
//            long currentTime = System.currentTimeMillis();
//            long sevenDaysAgo = currentTime - (7 * 24 * 60 * 60 * 1000L); // 7天前的毫秒数
//
//            long fileModifiedTime = FileUtils.getFileLastModified(file);
//            if (fileModifiedTime < sevenDaysAgo) {
//                // 文件修改时间超过7天，删除文件
//                FileUtils.delete(file);
//                continue;
//            }

            String phoneTime = TimeUtils.millis2String(FileUtils.getFileLastModified(file), "yyyy年MM月dd日");

            List<PhoneAlbumBean.ClassBean> beanList = null;
            if (timeCategoryMap.containsKey(phoneTime)) {
                beanList = timeCategoryMap.get(phoneTime);
            } else {
                beanList = new ArrayList<>();
                timeCategoryMap.put(phoneTime, beanList);
            }

            PhoneAlbumBean.ClassBean classBean = new PhoneAlbumBean.ClassBean(file.getName(),
                    file.getPath(), FileUtils.getFileLastModified(file));
            beanList.add(classBean);
        }
        Iterator<String> iterator = timeCategoryMap.keySet().iterator();
        String time = null;
        while (iterator.hasNext()) {
            time = iterator.next();
            classBeanList.add(new PhoneAlbumBean(time, timeCategoryMap.get(time)));
        }
        SortUtils.getSortShotBefore(classBeanList);
        return classBeanList;
    }

    private List<File> getHistoryPic(String path) {
        boolean orExistsDir = FileUtils.createOrExistsDir(PathUtils.getFilesPathExternalFirst() + path);
        if (orExistsDir) {
            return FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + path);
        }
        return null;
    }

}
